using Microsoft.AspNetCore.Mvc;
using UnitTest.Model;
using UnitTest.Service;

namespace UnitTestDemo.Controller;



[Route("api/[controller]")]
[ApiController]
public class StudentController: ControllerBase
{
    public  IStudentService _studentService;

    public StudentController(IStudentService studentService)
    {
        _studentService = studentService;
    }

    public List<Student> _student;
    public StudentController(List<Student> student)
    {
        _student = student;
    }
    
    public Student _student1;
    public StudentController(Student student)
    {
        _student1 = student;
    }

    [HttpGet("Test")]
    public void Test()
    {
       
    }
    
    [HttpGet("{id}")]
    public async Task<Student> Get(int id)
    {
        var res = await _studentService.GetById(id);
        Console.WriteLine(res.Name);
        return res;
    }

    [HttpGet]
    public async Task<List<Student>> GetAllStudent()
    {
        var res = await _studentService.GetList();
        return res;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] Student student)
    {
        var res = await _studentService.Post(student);
        return res>0? Ok() : BadRequest();
    }
    
    [HttpPost("Post_Test")]
    public async Task<bool> Post_Test([FromBody] Student student)
    {
        var res = await _studentService.Post(student);
        return res>0?true:false;
    }

    [HttpDelete]
    public async Task<bool> Delete(int id)
    {
        if (id > 0)
        {
            var res = await _studentService.GetById(id);
            if (res!=null)
            {
                var re = _studentService.Delete(res);
                return true;

            }
        }

        return false;
    }
    
    
    
}